﻿<?php

    class Validator
    {
        
        public static function validateUser($username, $excep = true, $empty = true)
        {
            if($empty && empty($username))
                return 1;
            
            if(!$username)
            {
                if($excep)
                    throw new Exception('Tham số không hợp lệ.');
                else
                    return false;
            }
            
            $userpattern = '/^[a-zA-Z0-9-_\.]{3,16}/';
            
            if(!preg_match($userpattern, $username))
            {
                if($excep)
                    throw new Exception('Username phải là các chữ cái hay số và từ 3-16 ký tự.');
                else
                    return false;
            }
            
            return true;
        }
        
        public static function validatePassword($pass, $excep = true, $empty = true){
            
            if($empty && empty($pass))
                return 1;
            
            if(!$pass)
            {
                if($excep)
                    throw new Exception('Tham số không hợp lệ.');
                else
                    return false;
            }
            
            $passpattern = '/^[a-zA-Z0-9]{3,16}/';
            
            if(!preg_match($passpattern, $pass))
            {
                if($excep)
                    throw new Exception('Password phải là các chữ cái hay số và từ 3-16 ký tự.');
                else
                    return false;
            }
            
            return true;
        }
        
        function validateEmail($object, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            $pattern = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
            return preg_match($pattern, $object);
        }
        
        function validateUrl($object, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            $pattern = '/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i';
            return preg_match($pattern, $object);
        }
        
        function validateNumber($object, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            $pattern = '/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/';
            return preg_match($pattern, $object);
        }
        
        function validateInteger($object, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            $pattern = '/(^-?\d\d*$)/';
            return preg_match($pattern, $object);
        }
        
        function validateAlpha($object, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            $pattern = '/^[a-zA-Z]+$/';
            return preg_match($pattern, $object);
        }
        
        function validateAlphaNum($object, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            $pattern = '/\w/';
            return preg_match($pattern, $object);
        }
        
        function validateMinLength($object, $length, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            $iv = strlen($object);
            return $iv >= $length;
        }
        
        function validateMaxLength($object, $length, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            $iv = strlen($object);
            return $iv <= $length;
        }
        
        function validateRequired($object, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            return !empty($object) && $object;
        }
        
        function validateDate($object, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            $date = split("/", $object);
            if(count($date) == 3)
            {
                $month = intval($date[1]);
                $day = intval($date[0]);
                $year = intval($date[2]);
                
                var_dump($month);
                
                if($year < 1900 || $year > 2000 || $day > 31 || $day < 0 || $month > 12 || $month < 0)
                    return false;
                
                $maxday = 0;
                if($month == 2){
                    if($year % 400 == 0 || $year % 4 == 0 && $year % 100 > 0)
                        $maxday = 29;
                    else
                        $maxday = 28;
                }
                else if($month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12)
                    $maxday = 31;
                else 
                    $maxday = 30;
                
                if($day > $maxday)
                    return false;
                
                return true;
            }
            else
                return false;
        }
        
        function validatePhone($object, $empty = true) 
        {
            if($empty && empty($object))
                return 1;
            
            return Validator::validateNumber($object) && Validator::validateMinLength($object, 6) && Validator::validateMaxLength($object, 12);
        }
        
        function validate($object, $type)
        {
            switch ($type) 
            {
                case 'password':
                    return Validator::validatePassword($object, false);
                case 'username':
                    return Validator::validateUser($object, false);
                case 'email':
                    return Validator::validateEmail($object);
                case 'date':
                    return Validator::validateDate($object);
                case 'phone':
                    return Validator::validatePhone($object);
                case 'number':
                    return Validator::validateNumber($object);
                case 'alpha':
                    return Validator::validateAlpha($object);
                case 'alphanum':
                    return Validator::validateAlphaNum($object);
                case 'maxlength':
                    return Validator::validateMaxLength($object);
                case 'minlength':
                    return Validator::validateMaxLength($object);
                case 'required':
                    return Validator::validateRequired($object);
                
                default:
                    break;
            }
        }
    }

?>
